package com.sc.userspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.userspringboot.pojo.PostCollections;
import com.sc.userspringboot.pojo.Posts;
import com.sc.userspringboot.service.IPostCollectionsService;
import com.sc.userspringboot.service.IPostsService;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户帖子收藏关联表 前端控制器
 * </p>
 *
 * @author user
 * @since 2025-09-19 06:25:46
 */
@RestController
@RequestMapping("/postCollections")
public class PostCollectionsController {
    @Autowired
    private IPostCollectionsService postCollectionsService;
    @Autowired
    private IPostsService iPostsService;

    @GetMapping("getUserPostsAndAuthor")
    public ResponseMsg getUserPostsAndAuthor(Integer userId) {
        QueryWrapper<PostCollections> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<PostCollections> posts = postCollectionsService.list(queryWrapper);
        List<Integer> postIds = posts.stream()
                .map(PostCollections::getPostId)
                .collect(Collectors.toList());
        if (postIds.size() == 0) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", new ArrayList<>());
        }
        List<Posts> userPosts = iPostsService.getUserPostsAndAuthor(postIds);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", userPosts);
    }
}
